package com.Telien.app.ui.user;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import org.apache.commons.lang3.StringUtils;

import net.miginfocom.swing.MigLayout;

import com.Telien.app.user.service.UserManagementService;

public class NewUserDialog extends JDialog implements ActionListener{

	private final JPanel contentPanel = new JPanel();
	private JTextField txtFirstName;
	private JTextField txtLastName;
	private JTextField txtUserName;
	private JPasswordField txtPassword;
	private JPasswordField txtConfirmPassword;
	private JTextField txtAddress;
	private JTextField txtPhoneNumber;
	private JTextField txtEmailAddress;
	private JTextField txtConfirmEmailAddress;
	private JButton okButton;
	private JButton cancelButton;
	private JLabel errorMsgLabel;

	/**
	 * Launch the application.
	 *
	public static void main(String[] args) {
		try {
			NewUserDialog dialog = new NewUserDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public NewUserDialog() {
		setBounds(100, 100, 531, 342);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblFirstName = new JLabel("First Name:");
			lblFirstName.setBounds(10, 38, 67, 16);
			contentPanel.add(lblFirstName);
		}
		{
			txtFirstName = new JTextField();
			txtFirstName.setBounds(114, 35, 116, 22);
			txtFirstName.setText("First Name");
			txtFirstName.addActionListener(this);
			contentPanel.add(txtFirstName);
			txtFirstName.setColumns(10);
		}
		{
			JLabel lblLastName = new JLabel("Last Name:");
			lblLastName.setBounds(237, 38, 65, 16);
			contentPanel.add(lblLastName);
		}
		{
			txtLastName = new JTextField();
			txtLastName.setBounds(314, 35, 116, 22);
			txtLastName.setText("Last Name");
			contentPanel.add(txtLastName);
			txtLastName.setColumns(10);
		}
		{
			JLabel lblUserName = new JLabel("User Name:");
			lblUserName.setBounds(10, 67, 68, 16);
			contentPanel.add(lblUserName);
		}
		{
			txtUserName = new JTextField();
			txtUserName.setBounds(114, 64, 116, 22);
			txtUserName.setText("User Name");
			contentPanel.add(txtUserName);
			txtUserName.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Password:");
			lblPassword.setBounds(242, 67, 60, 16);
			contentPanel.add(lblPassword);
		}
		{
			txtPassword = new JPasswordField();
			txtPassword.setBounds(314, 64, 116, 22);
			txtPassword.setText("Password");
			contentPanel.add(txtPassword);
			txtPassword.setColumns(10);
		}
		{
			JLabel lblConfirmPassword = new JLabel("Confirm Password:");
			lblConfirmPassword.setBounds(242, 105, 60, 16);
			contentPanel.add(lblConfirmPassword);
		}
		{
			txtConfirmPassword = new JPasswordField();
			txtConfirmPassword.setBounds(314, 105, 116, 22);
			txtConfirmPassword.setText("Confirm Password");
			contentPanel.add(txtConfirmPassword);
			txtConfirmPassword.setColumns(10);
		}
		{
			JLabel lblAddress = new JLabel("Address:");
			lblAddress.setBounds(16, 134, 51, 16);
			contentPanel.add(lblAddress);
		}
		{
			txtAddress = new JTextField();
			txtAddress.setBounds(114, 131, 387, 22);
			txtAddress.setText("Address");
			contentPanel.add(txtAddress);
			txtAddress.setColumns(10);
		}
		{
			JLabel lblPhoneNumber = new JLabel("Phone Number:");
			lblPhoneNumber.setBounds(0, 105, 89, 16);
			contentPanel.add(lblPhoneNumber);
		}
		{
			txtPhoneNumber = new JTextField();
			txtPhoneNumber.setBounds(114, 96, 116, 22);
			txtPhoneNumber.setText("Phone Number");
			contentPanel.add(txtPhoneNumber);
			txtPhoneNumber.setColumns(10);
		}
		{
			JLabel lblEmailAddress = new JLabel("Email Address:");
			lblEmailAddress.setBounds(10, 163, 86, 16);
			contentPanel.add(lblEmailAddress);
		}
		{
			txtEmailAddress = new JTextField();
			txtEmailAddress.setBounds(114, 160, 187, 22);
			txtEmailAddress.setText("Email Address");
			contentPanel.add(txtEmailAddress);
			txtEmailAddress.setColumns(10);
		}
		{
			JLabel lblCofirmEmailAddress = new JLabel("Confirm Email Address");
			lblCofirmEmailAddress.setBounds(15, 198, 81, 16);
			contentPanel.add(lblCofirmEmailAddress);
		}
		{
			txtConfirmEmailAddress = new JTextField();
			txtConfirmEmailAddress.setBounds(114, 195, 187, 22);
			txtConfirmEmailAddress.setText("Email Address");
			contentPanel.add(txtConfirmEmailAddress);
			txtConfirmEmailAddress.setColumns(10);
		}
		{
			errorMsgLabel = new JLabel();
			errorMsgLabel.setBounds(30, 235, 81, 16);
			errorMsgLabel.setForeground(Color.RED);
			errorMsgLabel.setVisible(false);
			contentPanel.add(errorMsgLabel);			
		}
		{
			JPanel buttonPane = new JPanel();
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("Add");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
					}
				});
				okButton.setActionCommand("Cancel");
				okButton.addActionListener(this);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
			}
			buttonPane.setLayout(new MigLayout("", "[432px]", "[25px]"));
			buttonPane.add(okButton, "cell 0 0,growx,aligny top");
			buttonPane.add(cancelButton, "cell 0 0,alignx left,aligny top");
		}
	}
	
	public void actionPerformed(ActionEvent e){
		if (e.getSource() == cancelButton){
			dispose();
			JDialog userLogin = new UserLogInDialog();
			userLogin.setVisible(true);
		}		
		else if(e.getSource() == okButton){
			if (validateNewUser() ){
				UserManagementService ums = new UserManagementService();
				ums.addUser(txtUserName.getText(), txtPassword.getText(), txtFirstName.getText(), txtLastName.getText(), txtPhoneNumber.getText(), txtEmailAddress.getText());
				dispose();
				JDialog userLogin = new UserLogInDialog();
				userLogin.setVisible(true);
			}
		}
		else{
			JTextField field = (JTextField) e.getSource();
			field.setText("");
		}
	}
	
	public boolean validateNewUser(){
		boolean retVal = true;

		if(StringUtils.isBlank(txtPassword.getPassword().toString())){
			//password required
			errorMsgLabel.setText("Password required");
			errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			return false;
		}
		else if(StringUtils.isBlank(txtConfirmPassword.getPassword().toString())){
			//confirmation password required
			errorMsgLabel.setText("Confirmation Password required");
			errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			return false;
		}
		else if(StringUtils.isBlank(txtEmailAddress.getText())){
			//password required
			errorMsgLabel.setText("Email required");
			errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			return false;
		}
		else if(StringUtils.isBlank(txtConfirmEmailAddress.getText())){
			//confirmation email required
			errorMsgLabel.setText("Confirmation Email required");
			errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			return false;
		}

		if (! (txtPassword.getText().equals(txtConfirmPassword.getText()) ) ){
			//passwords don't match
			errorMsgLabel.setText("Passwords do not match.");
			retVal = false;
		}
		
		if (!txtEmailAddress.getText().equals(txtConfirmEmailAddress.getText() ) ){
			//emails don't match
			if (retVal){
				errorMsgLabel.setText("Email addressess do not match.");
			}
			else{
				errorMsgLabel.setText(errorMsgLabel.getText() + " Email addressess do not match.");
			}
			retVal = false;
		}
		
		if(!retVal){
			errorMsgLabel.setSize(errorMsgLabel.getPreferredSize());
			errorMsgLabel.setVisible(true);
		}
			
		return retVal;
	}
}
